package cn.demographic.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import cn.demographic.dao.ApplicanttableMapper;
import cn.demographic.dao.EmploymentMapper;
import cn.demographic.dao.FamilyMapper;
import cn.demographic.dao.LandlordMapper;
import cn.demographic.dao.LiveMapper;
import cn.demographic.dao.LivinginformationMapper;
import cn.demographic.dao.MarriageMapper;
import cn.demographic.dao.SpouseMapper;
import cn.demographic.dao.UserMapper;
import cn.demographic.entity.Applicanttable;
import cn.demographic.entity.Employment;
import cn.demographic.entity.Family;
import cn.demographic.entity.Landlord;
import cn.demographic.entity.Live;
import cn.demographic.entity.Livinginformation;
import cn.demographic.entity.Marriage;
import cn.demographic.entity.Spouse;
import cn.demographic.entity.User;
import cn.demographic.service.PersonalInformationService;
/**
 * 插入数据
 * @author Administrator
 *
 */
@Service
public class PersonalInformationServiceImpl implements PersonalInformationService{
	@Resource
	private UserMapper userMapper;
	@Resource
	private LiveMapper liveMapper;
	@Autowired
	private MarriageMapper marriageMapper;
	@Autowired
	private SpouseMapper spouseMapper;
	@Autowired
	private ApplicanttableMapper applicanttableMapper;
	@Autowired
	private FamilyMapper familymapper;
	@Autowired
	private EmploymentMapper employmentMapper;
	@Resource
	private LivinginformationMapper livinginfomationMapper;
	@Resource
	private LandlordMapper landlordMapper;
	
	/**
	 * 添加个人信息+地址信息添加
	 * @param user
	 * @param live
	 * @return
	 */
	public Integer addPersonal(User user,Live live,Live live2) {
		/*首先添加地址信息返回地址信息添加的主键ID*/
		Integer livehouseholdId = 0;	//户籍
		Integer livenowId = 0;
		liveMapper.addLivePersonal(live);
		livehouseholdId = live.getId();
		liveMapper.addLivePersonal(live2);
		livenowId = live2.getId();
		/*然后在添加个人信息*/
		user.setLivehouseholdid(livehouseholdId);
		user.setLivenowid(livenowId);
		user.setEnterTime( new Date() );
		Integer count = userMapper.addPersonal(user);
		/*添加审核信息*/
		Applicanttable applicanttable = new Applicanttable();
		applicanttable.setUserid( user.getId() );
		applicanttable.setFilltimeid( new Date() );
		applicanttable.setStatus(0);
		applicanttableMapper.addApplicanttable(applicanttable);
		return count ;
	}
	
	/**
	 * 添加计生信息
	 */
	@Override
	public int insertSelective(Marriage record) {
		return marriageMapper.insertSelective(record);
	}

	/**
	 * 添加配偶信息
	 */
	@Override
	public int insertSelective(Spouse record,Live live) {
		liveMapper.addLivePersonal(live);
		record.setLiveid( live.getId() );
		return spouseMapper.insertSelective(record);
	}
	
	/**
	 * 添加同住信息
	 */
	public int insertFamily(@RequestParam(value = "betweenId",defaultValue = "0",required = false)Integer [] betweenId,
			String [] citizenship,String[] name,
			Integer[] gender,String[] birth,
			Integer[] certificate,
			Integer [] thisCity,
			Family family) {
		Integer count = 0;
		if(betweenId!=null&&citizenship!=null&&name!=null&&gender!=null&&birth!=null&&certificate!=null&&thisCity!=null) {
			for(int i=0;i<betweenId.length;i++) {
				if( betweenId[i] != 0 ) {
					family.setNumber(new Integer(i+1));
					family.setBetweenId(betweenId[i]);
					family.setCitizenship(citizenship[i]);
					family.setName(name[i]);
					family.setGender(gender[i]);
					if( birth[i]!=null && birth[i]!="" ) {
						SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						try {
							family.setBirth(dateFormat.parse(birth[i]));
						} catch (ParseException e) {
							e.printStackTrace();
						}
					}
					family.setCertificate(certificate[i]);
					family.setThisCity(thisCity[i]);
					family.setEnterTime(new Date());
					count= familymapper.insertFamily(family);
				}
			}
		}
		return count;
	}
	
	/**
	 * 添加就业信息
	 */
	@Override
	public Integer addemployment(Employment employment, Live live) {
		//添加地址信息
		liveMapper.addLivePersonal(live);
		//添加就业信息
		employment.setLiveId( live.getId() );
		employment.setEnterTime(new Date());
		Integer count = employmentMapper.addemployment(employment);
		return count;
	};
	
	
	/**
	 * 添加流动人口
	 */
	public void addliveinfo(Livinginformation livinginformation,
			Landlord landlord, Live live, HttpSession session) {
		/*添加个人信息*/
		User user = (User) session.getAttribute("user");
		Live liveUser1 = (Live) session.getAttribute("liveUser");
		Live liveUser2 = (Live) session.getAttribute("liveUser2");
		user.setEnterUser( livinginformation.getInfomation() );
		Integer countUser = addPersonal(user,liveUser1,liveUser2);
		/*配偶信息*/
		Spouse spouse = (Spouse) session.getAttribute("spouse");
		Live liveSpouse = (Live) session.getAttribute("liveSpouse");
		spouse.setUserid( user.getId() );
		spouse.setEnterTime(new Date());
		spouse.setEnterUser( livinginformation.getInfomation() );
		Integer countSpouse = insertSelective(spouse, liveSpouse);
		/*同住信息*/
		Integer[] betweenId = (Integer[]) session.getAttribute("betweenId");
		String[] citizenship = (String[]) session.getAttribute("citizenship");
		String[] name = (String[]) session.getAttribute("name");
		Integer[] gender = (Integer[]) session.getAttribute("gender");
		String[] birth = (String[]) session.getAttribute("birth");
		Integer[] certificate = (Integer[]) session.getAttribute("certificate");
		Integer[] thisCity = (Integer[]) session.getAttribute("thisCity");
		Family family = new Family();
		family.setUserid( user.getId() );
		family.setEnterUser( livinginformation.getInfomation() );
		Integer countFamily = insertFamily(betweenId, citizenship, name, gender, birth, certificate, thisCity,family);
		/*计生信息*/
		Marriage marriage = (Marriage) session.getAttribute("record");
		marriage.setUserid( user.getId() );
		marriage.setIsdel(0);
		marriage.setEnterTime(new Date());
		marriage.setEnterUser( livinginformation.getInfomation() );
		Integer countMarriage = insertSelective( marriage );
		/*就业信息*/
		Employment employment = (Employment) session.getAttribute("employment");
		Live liveEmployment = (Live) session.getAttribute("liveEmployment");
		employment.setUserid( user.getId() );
		employment.setEnterTime( new Date() );
		employment.setEnterUser( livinginformation.getInfomation() );
		Integer countEmployment = addemployment(employment, liveEmployment);
		/*居住信息*/
		liveMapper.addLivePersonal(live);
		landlordMapper.addland(landlord);
		livinginformation.setUserId( user.getId() );
		livinginformation.setHouseaddress(live.getId());
		livinginformation.setLandlorid(landlord.getId());
		livinginformation.setEnterTime(new Date());
		Integer count = livinginfomationMapper.addliveinfo(livinginformation);
		/*判断添加是否成功*/
		if( count==1 && countUser==1 && countFamily==1 && countMarriage==1 && countEmployment==1 && countSpouse==1) {
			//成功执行
		}else {
			//执行失败
		}
	}
}
